//
//  NewfeatureViewController.swift
//  MySinaWeiBo
//
//  Created by zhangfuwei on 15/7/30.
//  Copyright © 2015年 zhangfuwei. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class NewfeatureViewController: UICollectionViewController {

    private let layout = ZviewFlowlayout()
    private let imageCount = 4
    
    
    init(){
        super.init(collectionViewLayout: layout)
    }
    /// 禁止从 sb 中加载
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
       // MARK: UICollectionViewDataSource
    override func viewDidLoad() {
        super.viewDidLoad()
        // Register cell classes
        self.collectionView!.registerClass(FWcollectonViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        
    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return imageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! FWcollectonViewCell
    
        // Configure the cell
        cell.imageIndex = indexPath.item
        //  设置开始按钮的隐藏属性
        cell.startBtn.hidden = !(indexPath.item == imageCount - 1)
    
        return cell
    }
    
    //  MARK: cell完全显示后执行按钮动画
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        let path = collectionView.indexPathsForVisibleItems().last
        if path?.item == imageCount - 1 {
            let cell = collectionView.cellForItemAtIndexPath(path!) as! FWcollectonViewCell
            cell.startBtnAnim()
        }
    }

 }


// MARK:  自定义的 cell
 class FWcollectonViewCell : UICollectionViewCell {
    
    /// 开始体验
    func start() {
        print("开始体验--切换到主控制器")
        NSNotificationCenter.defaultCenter().postNotificationName(FWNotification, object: true)
        
    }
    
    //  定义一个图片索引,并重写其 setter 方法
    var imageIndex : Int = 0  {
        
        didSet {
            backView = UIImageView(image: UIImage(named: "new_feature_\(imageIndex + 1)"))
            prepareUI()
        }
    }
    
    func startBtnAnim() {
        
        startBtn.transform = CGAffineTransformScale(self.startBtn.transform,0,0)
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.startBtn.transform = CGAffineTransformIdentity

            }, completion: nil)
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }

    
    required init?(coder aDecoder: NSCoder) {
        // 从 sb 加载 cell
        super.init(coder: aDecoder)
        prepareUI()
    }
    
    func prepareUI() {
        backgroundView = backView
        contentView.addSubview(startBtn)
        
        //  设置约束
        startBtn.translatesAutoresizingMaskIntoConstraints = false
        startBtn.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: contentView, size:nil, offset: CGPoint(x: 0, y: -160))
    }
    
    
    
   private lazy var backView = UIImageView()
   private lazy var  startBtn : UIButton = {
    
    let startBtn = UIButton()
    
    startBtn.setTitle("开始体验", forState: UIControlState.Normal)

    startBtn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)

    startBtn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
    startBtn.sizeToFit()

    startBtn.addTarget(self, action: "start", forControlEvents: UIControlEvents.TouchUpInside)
    
    return startBtn

    }()
 
}





// MARK:  自定义的流水布局
private  class ZviewFlowlayout: UICollectionViewFlowLayout {
    private override func prepareLayout() {
        
        itemSize = (collectionView?.bounds.size)!
        minimumInteritemSpacing = 0
        minimumLineSpacing = 0
        scrollDirection = UICollectionViewScrollDirection.Horizontal
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        
    }
}



